package com.x.schedule.Filter;

import com.x.schedule.pojo.SysUser;
import jakarta.servlet.*;
import jakarta.servlet.annotation.WebFilter;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;

/**
 * ClassName: loginFilter
 * Package: IntelliJ IDEA
 * Description:
 * Written by HanXu
 * Create 2024.08.30 23:33
 */

//对showSchedule.html和SysScheduleController.java过滤拦截
@WebFilter(urlPatterns = {"/showSchedule.html", "/schedule/*"})
public class loginFilter implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

        //获得Session域对象
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpSession session = request.getSession();

        //在Session域中查找是否存在用户对象
        SysUser sysUser = (SysUser) session.getAttribute("sysUser");


        //判断用户对象是否为null
            //没登录，跳转至login.html
            //登陆过，放行
        if(sysUser==null){
            response.sendRedirect("/login.html");
        }else {
            filterChain.doFilter(servletRequest, servletResponse);
        }

    }
}
